home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Game Programming in C++ - Start to Finish
/
GameProgrammingS.iso
/
Peon
/
PeonSDK-Win32-1.0.0.exe
/
{app}
/
PeonMain
/
source
/
SceneCamera.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2005-10-26
|
8KB
|
249 lines
#include "EngineCore.h"
#include "SceneCamera.h"
namespace peon
{
SceneCamera::SceneCamera()
{
}
SceneCamera::~SceneCamera()
{
}
void SceneCamera::setPerspectiveProj( float fAspect, float z_min, float z_max )
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0f, fAspect, z_min, z_max );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
void SceneCamera::setViewMatrix( Vector3& vecEye, Vector3& vecLookAt, Vector3& vecUp)
{
m_vecPos = vecEye;
m_vecUp = vecUp;
m_vecLookAt = vecLookAt;
}
void SceneCamera::updateView()
{
gluLookAt( m_vecPos.x, m_vecPos.y, m_vecPos.z,
m_vecLookAt.x, m_vecLookAt.y, m_vecLookAt.z,
m_vecUp.x, m_vecUp.y, m_vecUp.z );
}
void SceneCamera::onMouseEvent(SDL_Event* pEvent)
{
int middleX = EngineCore::getSingleton().getRenderer()->getWidth() >> 1;
int middleY = EngineCore::getSingleton().getRenderer()->getHeight() >> 1;
if(pEvent->type == SDL_MOUSEMOTION )
{
int mousePos_x,mousePos_y;
//int middleX = SCREEN_WIDTH >> 1; // This is a binary shift to get half the width
//int middleY = SCREEN_HEIGHT >> 1; // This is a binary shift to get half the height
float deltaY = 0.0f; // This is the direction for looking up or down
float rotateY = 0.0f; // This will be the value we need to rotate around the Y axis (Left and Right)
// Get the mouse's current X,Y position
SDL_GetMouseState(&mousePos_x,&mousePos_y);
// If our cursor is still in the middle, we never moved... so don't update the screen
if( (mousePos_x == middleX) && (mousePos_y == middleY) ) return;
// Set the mouse position to the middle of our window
SDL_WarpMouse(middleX, middleY);
// Get the direction the mouse moved in, but bring the number down to a reasonable amount
rotateY = (float)( (middleX - mousePos_x) ) / 1000;
deltaY = (float)( (middleY - mousePos_y) ) / 1000;
// Multiply the direction vVector for Y by an acceleration (The higher the faster is goes).
m_vecLookAt.y += deltaY * 8;
// Check if the distance of our view exceeds 10 from our position, if so, stop it. (UP)
if( ( m_vecLookAt.y - m_vecPos.y ) > 10) m_vecLookAt.y = m_vecPos.y + 10;
// Check if the distance of our view exceeds -10 from our position, if so, stop it. (DOWN)
if( ( m_vecLookAt.y - m_vecPos.y ) < -10) m_vecLookAt.y = m_vecPos.y - 10;
// Here we rotate the view along the X avis depending on the direction (Left of Right)
rotateView(0, -rotateY, 0);
}
}
void SceneCamera::rotateView(float X, float Y, float Z)
{
Vector3 vVector; // Vector for the position/view.
// Get our view vVector (The direciton we are facing)
vVector.x = m_vecLookAt.x - m_vecPos.x; // This gets the direction of the X
vVector.y = m_vecLookAt.y - m_vecPos.y; // This gets the direction of the Y
vVector.z = m_vecLookAt.z - m_vecPos.z; // This gets the direction of the Z
// Rotate the view along the desired axis
if(X)
{
// Rotate the view vVector up or down, then add it to our position
m_vecLookAt.z = (float)(m_vecPos.z + sin(X)*vVector.y + cos(X)*vVector.z);
m_vecLookAt.y = (float)(m_vecPos.y + cos(X)*vVector.y - sin(X)*vVector.z);
}
if(Y)
{
// Rotate the view vVector right or left, then add it to our position
m_vecLookAt.z = (float)(m_vecPos.z + sin(Y)*vVector.x + cos(Y)*vVector.z);
m_vecLookAt.x = (float)(m_vecPos.x + cos(Y)*vVector.x - sin(Y)*vVector.z);
}
if(Z)
{
// Rotate the view vVector diagnally right or diagnally down, then add it to our position
m_vecLookAt.x = (float)(m_vecPos.x + sin(Z)*vVector.y + cos(Z)*vVector.x);
m_vecLookAt.y = (float)(m_vecPos.y + cos(Z)*vVector.y - sin(Z)*vVector.x);
}
}
void SceneCamera::generateViewFrustum()
{
/*
float p[16]; // projection matrix
float mv[16]; // model-view matrix
float mvp[16]; // model-view-projection matrix
glGetFloatv( GL_PROJECTION_MATRIX, p );
glGetFloatv( GL_MODELVIEW_MATRIX, mv );
//
// Concatenate the projection matrix and the model-view matrix to produce
// a combined model-view-projection matrix.
//
mvp[ 0] = mv[ 0] * p[ 0] + mv[ 1] * p[ 4] + mv[ 2] * p[ 8] + mv[ 3] * p[12];
mvp[ 1] = mv[ 0] * p[ 1] + mv[ 1] * p[ 5] + mv[ 2] * p[ 9] + mv[ 3] * p[13];
mvp[ 2] = mv[ 0] * p[ 2] + mv[ 1] * p[ 6] + mv[ 2] * p[10] + mv[ 3] * p[14];
mvp[ 3] = mv[ 0] * p[ 3] + mv[ 1] * p[ 7] + mv[ 2] * p[11] + mv[ 3] * p[15];
mvp[ 4] = mv[ 4] * p[ 0] + mv[ 5] * p[ 4] + mv[ 6] * p[ 8] + mv[ 7] * p[12];
mvp[ 5] = mv[ 4] * p[ 1] + mv[ 5] * p[ 5] + mv[ 6] * p[ 9] + mv[ 7] * p[13];
mvp[ 6] = mv[ 4] * p[ 2] + mv[ 5] * p[ 6] + mv[ 6] * p[10] + mv[ 7] * p[14];
mvp[ 7] = mv[ 4] * p[ 3] + mv[ 5] * p[ 7] + mv[ 6] * p[11] + mv[ 7] * p[15];
mvp[ 8] = mv[ 8] * p[ 0] + mv[ 9] * p[ 4] + mv[10] * p[ 8] + mv[11] * p[12];
mvp[ 9] = mv[ 8] * p[ 1] + mv[ 9] * p[ 5] + mv[10] * p[ 9] + mv[11] * p[13];
mvp[10] = mv[ 8] * p[ 2] + mv[ 9] * p[ 6] + mv[10] * p[10] + mv[11] * p[14];
mvp[11] = mv[ 8] * p[ 3] + mv[ 9] * p[ 7] + mv[10] * p[11] + mv[11] * p[15];
mvp[12] = mv[12] * p[ 0] + mv[13] * p[ 4] + mv[14] * p[ 8] + mv[15] * p[12];
mvp[13] = mv[12] * p[ 1] + mv[13] * p[ 5] + mv[14] * p[ 9] + mv[15] * p[13];
mvp[14] = mv[12] * p[ 2] + mv[13] * p[ 6] + mv[14] * p[10] + mv[15] * p[14];
mvp[15] = mv[12] * p[ 3] + mv[13] * p[ 7] + mv[14] * p[11] + mv[15] * p[15];
//
// Extract the frustum's right clipping plane and normalize it.
//
m_oFrustumPlanes[0].a = mvp[ 3] - mvp[ 0];
m_oFrustumPlanes[0].b = mvp[ 7] - mvp[ 4];
m_oFrustumPlanes[0].c = mvp[11] - mvp[ 8];
m_oFrustumPlanes[0].d = mvp[15] - mvp[12];
m_oFrustumPlanes[0].normalize();
//
// Extract the frustum's left clipping plane and normalize it.
//
m_oFrustumPlanes[1].a = mvp[ 3] + mvp[ 0];
m_oFrustumPlanes[1].b = mvp[ 7] + mvp[ 4];
m_oFrustumPlanes[1].c = mvp[11] + mvp[ 8];
m_oFrustumPlanes[1].d = mvp[15] + mvp[12];
m_oFrustumPlanes[1].normalize();
//
// Extract the frustum's bottom clipping plane and normalize it.
//
m_oFrustumPlanes[2].a = mvp[ 3] + mvp[ 1];
m_oFrustumPlanes[2].b = mvp[ 7] + mvp[ 5];
m_oFrustumPlanes[2].c = mvp[11] + mvp[ 9];
m_oFrustumPlanes[2].d = mvp[15] + mvp[13];
m_oFrustumPlanes[2].normalize();
//
// Extract the frustum's top clipping plane and normalize it.
//
m_oFrustumPlanes[3].a = mvp[ 3] - mvp[ 1];
m_oFrustumPlanes[3].b = mvp[ 7] - mvp[ 5];
m_oFrustumPlanes[3].c = mvp[11] - mvp[ 9];
m_oFrustumPlanes[3].d = mvp[15] - mvp[13];
m_oFrustumPlanes[3].normalize();
//
// Extract the frustum's far clipping plane and normalize it.
//
m_oFrustumPlanes[4].a = mvp[ 3] - mvp[ 2];
m_oFrustumPlanes[4].b = mvp[ 7] - mvp[ 6];
m_oFrustumPlanes[4].c = mvp[11] - mvp[10];
m_oFrustumPlanes[4].d = mvp[15] - mvp[14];
m_oFrustumPlanes[4].normalize();
//
// Extract the frustum's near clipping plane and normalize it.
//
m_oFrustumPlanes[5].a = mvp[ 3] + mvp[ 2];
m_oFrustumPlanes[5].b = mvp[ 7] + mvp[ 6];
m_oFrustumPlanes[5].c = mvp[11] + mvp[10];
m_oFrustumPlanes[5].d = mvp[15] + mvp[14];
m_oFrustumPlanes[5].normalize();
*/
}
bool SceneCamera::isSphereInFrustum( float x, float y, float z, float fRadius )
{
/*
for( int i = 0; i < 6; ++i )
{
if( m_oFrustumPlanes[i].a * x +
m_oFrustumPlanes[i].b * y +
m_oFrustumPlanes[i].c * z +
m_oFrustumPlanes[i].d <= -fRadius )
return false;
}
*/
return true;
}
}